package com.thumbnails;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ImageActivity extends Activity {

	
	private ImageLoader imageLoader = ImageLoader.getInstance();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		Intent intent= getIntent();
		String path= intent.getStringExtra(Keys.PATH);
		Bitmap bitmap= BitmapFactory.decodeFile(path);
		File imageFile= new File(path);
		if(imageFile.exists()) {
			
			ImageView image= (ImageView) findViewById(R.id.imageView);
			image.setImageResource(R.drawable.ic_launcher);
			loadImage(image, path);
		}
	}
	
	
	public static Bitmap changeImageColor(Bitmap bitmap,int newColor){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        Bitmap newImg = null;
        try{
            newImg = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(newImg);
            Paint paint = new Paint();

            paint.setColorFilter(new LightingColorFilter(Color.TRANSPARENT, newColor));
            canvas.drawBitmap(bitmap,0,0, paint);

            bitmap.recycle();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return newImg;
    }
	
	private void loadImage(final ImageView imageView, String path) {

		// 图片保存系统路径
		File cacheDir = StorageUtils.getOwnCacheDirectory(ImageActivity.this, "data");
		DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
																			.cacheInMemory()
																			.cacheOnDisc()
																			.build();
		
//		DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
////		.showStubImage(R.drawable.ic_launcher)// 设置图片在下载期间显示的图片
//		.showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
//		.showImageOnFail(R.drawable.ic_launcher)// 设置图片加载/解码过程中错误时候显示的图片
//		.cacheInMemory(false)// 是否緩存都內存中
//		.cacheOnDisc(false)// 是否緩存到sd卡上
////		.displayer(new RoundedBitmapDisplayer(20))
//		.build();
		// 一些相关的设置
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
																	.threadPoolSize(5)
																	.threadPriority(Thread.NORM_PRIORITY - 1)
																	.tasksProcessingOrder(QueueProcessingType.LIFO)
																	.denyCacheImageMultipleSizesInMemory()
																	.memoryCache(new LruMemoryCache(1024 * 1024))
																	.memoryCacheSize(1 * 1024 * 1024)
																	.discCache(new UnlimitedDiscCache(cacheDir))
																	// default
																	.discCacheSize(20 * 1024 * 1024)
																	.discCacheFileCount(400)
																	.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
																	// default
																	.imageDownloader(new BaseImageDownloader(this))
																	.imageDecoder(new BaseImageDecoder(true))
																	.defaultDisplayImageOptions(imageOptions)
																	.build();
		// 初始化配置
		imageLoader.init(config);
		// 显示图片
		imageLoader.displayImage("file://" + path, imageView);
	}
	
	@Override
	protected void onStop() {

		super.onStop();
		imageLoader.clearDiscCache();
		imageLoader.clearMemoryCache();
	}
}
